//
//  FDBSDK.m
//  FDBSDK
//
//  Created by curer on 12/13/15.
//  Copyright © 2015 lion. All rights reserved.
//

#import "FDBSDK.h"
#import "AGWindowView.h"
#import "AFNetworking.h"

#import "FDBMainView.h"
#import "FDBContentViewController.h"
#import "FDBResource.h"
#import "FDBConfigure.h"

NSString *kFDBSDKDidReceiveFeedbackNotification = @"FDBSDKDidReceiveFeedbackNotification";

@interface FDBSDK ()

@property (nonatomic, strong) AGWindowView *container;

@property (nonatomic, strong) AFHTTPRequestOperationManager *httpManager;

@end

@implementation FDBSDK

+ (instancetype)sharedInstance {
    static FDBSDK *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [FDBSDK new];
    });
    
    return _sharedInstance;
}

#pragma mark - public

+ (void)setupCurrentWithUserId:(NSString *)userId info:(NSDictionary *)info {
    [FDBConfigure sharedInstance].userInfo = info;
    [FDBConfigure sharedInstance].userId = userId;
}

+ (void)setupApplicationId:(NSString *)appId clientKey:(NSString *)key {
    [FDBConfigure sharedInstance].appId = appId;
    [FDBConfigure sharedInstance].clientKey = key;
}

+ (void)sendFeedbackWithContent:(NSString *)content {
    [self sendFeedbackWithContent:content withEmotionLevel:0];
}

+ (void)sendFeedbackWithContent:(NSString *)content withEmotionLevel:(NSInteger)level {
    [[self sharedInstance] sendFeedbackWithContent:content withEmotionLevel:level];
}

+ (void)setupContact:(NSString *)contact withType:(FDBSDKContactType)type {
    if (contact.length == 0) {
        return;
    }
    [[FDBConfigure sharedInstance].contactDictionary setObject:contact
                                                        forKey:@(type)];
}

- (void)showInViewController:(UIViewController *)viewController {
    NSBundle *bundle = MYBUNDLE;
    FDBContentViewController *vc = [[FDBContentViewController alloc] initWithNibName:@"FDBContentViewController"
                                                                              bundle:bundle];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc];
    [viewController presentViewController:navVC
                                 animated:YES
                               completion:nil];
}

#pragma mark - private

- (instancetype)init {
    if (self = [super init]) {
//        _httpManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/api/"]];
        _httpManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://feedback.avosapps.com/api/"]];
    }
    
    return self;
}

- (void)sendFeedbackWithContent:(NSString *)content withEmotionLevel:(NSInteger)level {
    NSMutableDictionary *basic = [self paramDictionary];
    [basic setObject:content forKey:@"content"];
    [basic setObject:@(level) forKey:@"emotion_level"];
    
    [self.httpManager POST:@"feedback/create"
                parameters:basic
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {
                       NSLog(@"success\n %@", responseObject);
                   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       NSLog(@"%@", error);
                   }];
}

- (NSMutableDictionary *)paramDictionary {
    NSMutableDictionary *dic = [NSMutableDictionary new];
    if ([FDBConfigure sharedInstance].userId.length > 0) {
        [dic setObject:[FDBConfigure sharedInstance].userId forKey:@"user_id"];
    }
    
    [dic setObject:[FDBConfigure sharedInstance].appId forKey:@"app_id"];
    [dic setObject:[FDBConfigure sharedInstance].clientKey forKey:@"client_key"];
    NSArray *FDBSDKContactTypeStrings = @[@"contact_qq", @"contact_phone", @"contact_email"];
    
    NSDictionary *contactDic = [FDBConfigure sharedInstance].contactDictionary;
    for (NSNumber *item in contactDic) {
        FDBSDKContactType type = [item integerValue];
        [dic setObject:contactDic[item] forKey:FDBSDKContactTypeStrings[type]];
    }
    
    return dic;
}

- (void)showMainView {
    if ([[UIApplication sharedApplication] keyWindow] == nil) {
        NSLog(@"can not display at this time");
        return;
    }
    if (_container == nil) {
        _container = [[AGWindowView alloc] initAndAddToKeyWindow];
        _container.supportedInterfaceOrientations = AGInterfaceOrientationMaskAll;
    }
    
    FDBMainView *mainView = [[FDBMainView alloc] init];
    mainView.container = self.container;
    [_container bringToFront];
    [_container addSubviewAndFillBounds:mainView];
}

@end
